package com.rongshu.view.userInfo.activity;

import android.content.Intent;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.google.gson.Gson;
import com.rongshu.R;
import com.rongshu.utils.Common;
import com.rongshu.utils.Constant;
import com.rongshu.utils.LoginUtils;
import com.rongshu.utils.UserInfoUtils;
import com.rongshu.view.base.BaseActivity;
import com.rongshu.view.base.mvp.INetBean;
import com.rongshu.view.userInfo.entity.PostVipAuthentication;
import com.rongshu.view.userInfo.iview.AddVCertificationView;
import com.rongshu.view.userInfo.presenter.AddVCertificationPresenter;
import com.utils.SPUtils;
import com.utils.constants.SPKey;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

public class AddVCertificationActivity extends BaseActivity<AddVCertificationPresenter> implements AddVCertificationView {
    @BindView(R.id.txt_top_title)
    TextView txt_top_title;
    @BindView(R.id.txt_vip_explain)
    TextView txt_vip_explain;
    //intent
    private Intent intent = null;
    //bi_name,phone
    private String bi_name = null;
    private String mobile = null;
    private boolean isVip = false;
    //request
    private final int IDENTITY_AUTHENTICATION = 0x01;
    private final int MATERIALS_AUTHENTICATION = 0x02;
    //上传拼接的资料认证信息
    private PostVipAuthentication authentication = null;
    //map
    private Map<String, Object> map = null;

    @Override
    protected int initLayout() {
        return R.layout.activity_add_v_certification_view;
    }

    @Override
    protected void initView() {
        //presenter
        setPresenter(new AddVCertificationPresenter(this), this);
        //title
        txt_top_title.setText("认证信息");
        //intent
        intent = getIntent();
        if (intent != null) {
            bi_name = intent.getStringExtra(Constant.INTENT_MESSAGE);
            mobile = intent.getStringExtra(Constant.INTENT_DATA);
            isVip = intent.getBooleanExtra(Constant.INTENT_TYPE, isVip);
        }
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void updateViews(boolean isRefresh) {

    }

    @OnClick({R.id.layout_back, R.id.layout_identity_authentication, R.id.layout_materials_authentication,
            R.id.txt_add_v_request})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.layout_back:
                showKeyboard(false);
                finish();
                break;
            case R.id.layout_identity_authentication://身份认证
                intent = new Intent(this, IdentityAuthenticationActivity.class);
                intent.putExtra(Constant.INTENT_MESSAGE, bi_name);
                intent.putExtra(Constant.INTENT_DATA, mobile);
                startActivityForResult(intent, IDENTITY_AUTHENTICATION);
                break;
            case R.id.layout_materials_authentication://资料认证
                intent = new Intent(this, MaterialsAuthenticationActivity.class);
                startActivityForResult(intent, MATERIALS_AUTHENTICATION);
                break;
            case R.id.txt_add_v_request://申请认证
                showKeyboard(false);
                postVipAuthentication();
                break;
        }
    }

    /**
     * 申请加V认证
     */
    public void postVipAuthentication() {
        //user_id
        int user_id = UserInfoUtils.getUserId();
        if (user_id == 0) {
            LoginUtils.toLogin(this);
            return;
        }
        String vipText = txt_vip_explain.getText().toString();
        if (TextUtils.isEmpty(vipText)) {
            toast("请输入认证信息");
            return;
        }
        if (authentication == null) {
            toast("请先上传资料认证");
            return;
        }
        //认证说明
        authentication.setVipText(vipText);
        map = new HashMap<>();
        map.put("user_id", user_id);
        presenter.postVipAuthentication(
                (int) map.get("user_id"),
                Common.encryptionSign(map),
                authentication);
        map.clear();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case MATERIALS_AUTHENTICATION://资料认证
                    authentication = (PostVipAuthentication) data.getSerializableExtra(Constant.INTENT_DATA);
                    String json = new Gson().toJson(authentication);
                    SPUtils.putPreferences(SPKey.MATERIALSAUTHENTICATION, json);
                    break;
            }
        }
    }

    /**
     * 加V返回
     *
     * @param bean
     */
    @Override
    public void onVipAuthentication(INetBean bean) {
        if (bean.getErrCode() == 0) {
            toast("提交成功");
            showKeyboard(false);
            finish();
        } else {
            toast(bean.getErrMsg());
        }
    }
}
